//
//  CaipuLikeClassifyViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CaipuLikeClassifyViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "FontSizeSingleTon.h"
#define Font [[FontSizeSingleTon shareFontSize]array]
#import "ZJHShakeDetailTableViewCell.h"
#import "ZJHShakeModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "Loading.h"
#import "ZJHShakeCaipuViewController.h"
#import "ZJHCaipuDetailViewController.h"
@interface CaipuLikeClassifyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UIImageView *leftImageView;
@property(nonatomic, strong)UILabel *titleLable;
@property(nonatomic, strong)UILabel *introLable;
@property(nonatomic, strong)UIImageView *rightImageView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)NSDictionary *foodDic;
@property(nonatomic, assign)NSInteger offset;
@property(nonatomic, strong)Loading *load;
@end

@implementation CaipuLikeClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = _tempModel.Title;
}
-(void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    _offset = 0;
    _dataArray = [NSMutableArray array];
    [self getData];
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _offset = 0;
        [self getData];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _offset += 20;
        [self getData];
    }];
    [self createProgressView];
}
//创建菊花
-(void)createProgressView
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < 70; i++) {
        [array addObject:[NSString stringWithFormat:@"%ld", i + 1]];
    }
    self.view.backgroundColor = [UIColor grayColor];
    _load = [[Loading alloc]initWithFrame:CGRectMake(0, 0, 375 * wt, 667 * ht - 64 - 49) PicArray:array];
    [self.view addSubview:_load];
}
//创建tableView顶视图
-(void)createHeaderView
{
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100 * ht)];
    _headerView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [_headerView addGestureRecognizer:tap];
    
    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10 * wt, 10 * ht, 80 * wt, 80 * wt)];
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:[_foodDic objectForKey:@"Cover"]]];
    [_headerView addSubview:_leftImageView];
    
    _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(_leftImageView.frame.origin.x + _leftImageView.frame.size.width + 10 * wt, _leftImageView.frame.origin.y + 5 * ht, 100 * wt, 20 * ht)];
    _titleLable.font = [UIFont systemFontOfSize:[Font [0]integerValue]];
    _titleLable.text = [_foodDic objectForKey:@"Name"];
    [_headerView addSubview:_titleLable];
    
    _introLable = [[UILabel alloc]initWithFrame:CGRectMake(_titleLable.frame.origin.x, _titleLable.frame.origin.y + _titleLable.frame.size.height + 10 * ht, 240 * wt, 50 * ht)];
    _introLable.numberOfLines = 0;
    _introLable.text = [_foodDic objectForKey:@"Intro"];
    _introLable.font = [UIFont systemFontOfSize:[Font[2]integerValue]];
    [_headerView addSubview:_introLable];
    
    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_introLable.frame.origin.x + _introLable.frame.size.width + 10 * wt, _leftImageView.frame.origin.y + 30 * ht, 20 * wt, 20 * ht)];
    rightImageView.image = [UIImage imageNamed:@"caipuxiangyou"];
    [_headerView addSubview:rightImageView];
    if (![[_foodDic objectForKey:@"Cover"] isEqualToString:@""]) {
        
        _tableView.tableHeaderView = _headerView;
    }
    
}

#pragma mark tableView顶视图手势方法
-(void)tapAction
{
    ZJHCaipuDetailViewController *vc = [[ZJHCaipuDetailViewController alloc]init];
    vc.foodId = [[_foodDic objectForKey:@"Id"] integerValue];
    vc.name = [_foodDic objectForKey:@"Name"];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)getData
{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSInteger date = time;
    NSString *post1 = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=150a69c233c62d5a87eee6a7ee7f6f32&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7CCC2D6423-97F6-437A-B8DE-CBB86A71C6F1%7C608CBAFB-426B-4C02-96CA-F5B029C17D76&format=json&loguid=&method=Search.getList&nonce=1446775731&sessionid=1446775737&signmethod=md5";
    NSString *post2 = [NSString stringWithFormat:@"timestamp=%ld&uuid=f54ccedd4a37e51b4340c5dc18fe353c&v=2&vc=43&vn=v5.4.0", date];
    NSString *postString = [NSString stringWithFormat:@"%@%@", post1, post2];
    NSString *bodyString = [NSString stringWithFormat:@"limit=20&offset=%ld&scene=&sign=&tagid=%ld&uid=&uuid=f54ccedd4a37e51b4340c5dc18fe353c", _offset, _tempModel.Id];
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {

        NSDictionary *dic = [data objectForKey:@"result"];
        _foodDic = [dic objectForKey:@"food"];
        NSArray *listArray = [dic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            ZJHShakeModel *model = [[ZJHShakeModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [self createHeaderView];
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_load removeFromSuperview];
    }];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ZJHShakeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZJHShakeDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.shakeModel = _dataArray[indexPath.row];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * ht;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJHShakeCaipuViewController *caipuVC = [[ZJHShakeCaipuViewController alloc]init];
    caipuVC.tempModel = _dataArray[indexPath.row];
    [self.navigationController pushViewController:caipuVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
